import 'dart:ffi' as ffi;
import 'dart:io';
import 'package:call/env.dart';
import 'package:flutter/material.dart';
import 'package:call/call.dart';
import 'package:ffi/ffi.dart';
import 'package:path/path.dart';

//ffi.Pointer<ffi.Int8>
typedef Callin = void Function();

typedef nnf = ffi.Void Function();
// Dart type definition for calling the C foreign function
typedef ddf = void Function();

 typedef nf = ffi.Void Function(ffi.Int8);
 typedef df = void Function(int);


// FFI signature of the hello_world C function
// typedef hello_world_func = ffi.Int8 Function(ffi.Void Function(ffi.Pointer<ffi.Int8>));
// // Dart type definition for calling the C foreign function
// typedef HelloWorld = int Function(ffi.Void Function(ffi.Pointer<ffi.Int8>));

typedef hello_world_func = ffi.Int8 Function(ffi.Pointer<ffi.NativeFunction<nf>>);
// Dart type definition for calling the C foreign function
typedef hello_world_dart = int Function(ffi.Pointer<ffi.NativeFunction<nf>>);

// The type 'Int Function(NativeFunction<Void Function(Pointer<Int8>)>)' 
// must be a subtype of 'int Function(void Function(Pointer<Int8>))'
//  for 'lookupFunction'.
void test_callback2(int result) {
    // String greeting = result.cast<Utf8>().toDartString();
  print(result);
  print("object");
}
  /// DynamicLibrary dylib = DynamicLibrary.executable();
  /// final add = dylib.lookupFunction<Int32 Function(Int32, Int32), int Function(int, int)>(
  ///         'add');
class TestSDK {
  int test_callback() {
    var result =
        globalLibray.lookupFunction<hello_world_func, hello_world_dart>('test_callback');
        return result(ffi.Pointer.fromFunction<nf>(test_callback2));
    // return result(ffi.NativeFunction<ss>);
  }
  // int test_callback3() {
  //  final ddf hello = globalLibray
  //     .lookup<ffi.NativeFunctionPointer<nnf>>('test_callback')
  //     .asFunction();
  // return hello(callbacka);
  // }

  // callbacka(ffi.Pointer<ffi.Int8> result) {
  //   String greeting = result.cast<Utf8>().toDartString();
  //   print(greeting);
  //   print("any");
  // }
  
  // callbackb() {
  //   print("any");
  // }

  static final TestSDK instance = TestSDK();
  
  final String _libPath = 'assets/libtest.so';
  String get _linuxPath {
    String getDebugPath() => join(Directory.current.path,
        'build/linux/x64/debug/bundle', 'data', 'flutter_assets');
    String getReleasePath() =>
        join(Directory.current.path, 'data', 'flutter_assets');
    return isProduct() ? getReleasePath() : getDebugPath();
  }

  static ffi.DynamicLibrary? _globalLibray;

  ffi.DynamicLibrary get globalLibray {
    if (_globalLibray != null) {
      return _globalLibray!;
    }
    late ffi.DynamicLibrary scalessLib;
    if (Platform.isLinux) {
      scalessLib = ffi.DynamicLibrary.open(join(_linuxPath, _libPath));
    } else {
      scalessLib = getDyLibModule(_libPath);
    }
    _globalLibray = scalessLib;
    return scalessLib;
  }

  //打开端口
  // function OPO_Open(sConnect: PAnsiChar): Integer; stdcall;
  // 参数说明
  // 	sConnect：	串口名称 + ":" + 波特率    或 串口名称（默认波特率115200）
  // 返回： 0：失败  1：成功
  // 例： com1:115200   或  com1
  int test_stringprint() {
    var result =
        globalLibray.lookupFunction<ffi.Int Function(ffi.Pointer<ffi.Int8>), int Function(ffi.Pointer<ffi.Int8>)>('test_stringprint');
    String sConnect = "ttyS1";
    var success = result(sConnect.toNativeUtf8().cast<ffi.Int8>());
    return success;
  }
  String test_stringreturn() {
    var result =
        globalLibray.lookupFunction<ffi.Pointer<ffi.Int8> Function(), ffi.Pointer<ffi.Int8> Function()>('test_stringreturn');
    var success = result();
    String greeting = success.cast<Utf8>().toDartString();
    return greeting;
  }
  int test_int() {
    var result =
        globalLibray.lookupFunction<ffi.Int Function(), int Function()>('test_int');
    return result();
  }
}
